package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.CmbB2bManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.CmbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2bMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2bPayResult;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2bQueryReq;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2bQueryRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

/** 
 * @ClassName: CmbB2bBankServiceImpl 
 * @Description:  招商b2b Service业务层处理对象 
 * @author w_jiqing 
 * @date 2016年8月29日 上午11:33:25  
 */ 
public class CmbB2bBankServiceImpl implements BankInteractRemoteService{
    @Resource(name = "cmbB2bManager")
    private CmbB2bManager cmbB2bManager;

    @Override
    public Result pay(BaseMessage base) {
        Result result =new Result(false);
        try{
            CmbB2bMessage messageB2b =new CmbB2bMessage();
            BeanUtils.copyProperties(base, messageB2b);
            messageB2b =(CmbB2bMessage) cmbB2bManager.sign(messageB2b);
            result.setDefaultModel(messageB2b);
            result.setSuccess(true);
        } catch(GopayException e){
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch(Exception ex){
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6028);
        }
        return result;
    }

    @Override
    public Result bankBack(BaseMessage base) {
        Result result=new Result(false);
        boolean flag = false;
        try{
            CmbB2bPayResult messageB2b=(CmbB2bPayResult) base;
            flag= (boolean) cmbB2bManager.verify(messageB2b);
            result.setModel("res", messageB2b);
            result.setDefaultModel(flag);
            result.setSuccess(true);
            if((boolean) result.getDefaultModel()){
                Map<String,String> param =new HashMap<String,String>();
                if(CmbConstants.CMBB2B_PAY_STATUS_SUCCESS.equals(messageB2b.getRtnflg())){
                    param.put("orderId", messageB2b.getOrderId());
                    param.put("amt", messageB2b.getAmount().toString()); 
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "00");
                    param.put("retCode", "00");
                    param.put("retMsg", "验签成功");
                    param.put("signData", "");
                    param.put("isFront", String.valueOf(base.isFront()));
                    result.setModel("isNotifyCashier", param);
                    result.setOrderStatus(true);
                }else{
                    param.put("orderId", messageB2b.getOrderId());
                    param.put("amt", messageB2b.getAmount().toString()); 
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "01");
                    param.put("retCode", "01");
                    param.put("retMsg", "验签失败");
                    param.put("signData", "");
                    param.put("isFront", String.valueOf(base.isFront()));
                    result.setModel("isNotifyCashier", param);
                    result.setOrderStatus(true);
                }
            }
        } catch(GopayException e){
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch(Exception ex){
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
        }
        return result;
    }

    @Override
    public Result queryBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result query(BaseMessage base) {
        Result result =new Result();
        try{
            CmbB2bQueryReq message =new CmbB2bQueryReq();
            BeanUtils.copyProperties(base,message);
            CmbB2bQueryRes res= (CmbB2bQueryRes) cmbB2bManager.query(message);
            result.setSuccess(true);
            result.setDefaultModel(res);
            if(BisPayCheckUtils.checkExternalOrderID(message.getOrderId())){
                Map<String,String> param =new HashMap<String,String>();
                param.put("orderId", message.getOrderId());
                param.put("amount", message.getAmount().toString());
                if(res == null){
                 // 查询订单为空
                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
                    param.put("returnMessage", "银行返回对账结果为空");
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    result.setModel("queryResult", param);
                    return result;
                }
                if(res.getNtqtrdqyz() != null){
                    if(CmbConstants.CMB_QUERY_STATUS_SUCCESS.equals(res.getNtqtrdqyz().getOrdordsta())){
                        param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                        //只有  Ordordsta = "FIN" ordstscod 值才有意义
                        String ordstscod = res.getNtqtrdqyz().getOrdstscod();                    
                        if(CmbConstants.CMBB2B_QUERY_STATUS_SUCCESS.equals(ordstscod)){
                          //成功处理
                            param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
                            param.put("returnMessage", "交易成功");
                        }else if(CmbConstants.CMBB2B_QUERY_STATUS_FAIL.equals(ordstscod)){
                            //返回失败
                            param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
                            param.put("returnMessage", "银行返回交易失败");
                        }else if(CmbConstants.CMBB2B_QUERY_EXPIRE.equals(ordstscod)){
                            //订单过期作废
                            param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                            param.put("returnMessage", "订单过期作废");
                        }else if(CmbConstants.CMBB2B_QUERY_NO_PAY.equals(ordstscod)){
                            //代缴费
                            param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                            param.put("returnMessage", "订单待缴费");
                        }else if(CmbConstants.CMBB2B_QUERY_PAY_NO_SUCCESS.equals(ordstscod)){
                            //多收方支付不成功
                            param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                            param.put("returnMessage", "多收方支付不成功");
                        }else{
                            param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                            param.put("returnMessage", "订单已撤销");
                        }
                        result.setModel("queryResult", param);
                    }else{
                        param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                        param.put("returnMessage", "交易进行中");
                        param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                        result.setModel("queryResult", param);
                    }
                }else{
                    param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                    param.put("returnMessage", "交易进行中");
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    result.setModel("queryResult", param);
                }                
            }
        }catch(GopayException e){
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        }catch(Exception ex){
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6032);
        }
        return result;
    }

    @Override
    public Result refund(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result dayend(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
